package com.lz.sports.dialog

import android.app.Dialog
import android.os.Bundle
import android.os.Parcelable
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.lz.library.adapter.SimpleAdapter
import com.lz.library.adapter.SimpleViewHolder
import com.lz.library.dialog.BaseDialogFragment
import com.lz.library.extension.click
import com.lz.sports.R
import com.lz.sports.databinding.DialogSelectBinding
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class SelectDialog : BaseDialogFragment<DialogSelectBinding>() {

    companion object {

        fun show(activity: FragmentActivity, title: String, position: Int, contents: ArrayList<String>, requestCode: Int = -1): SelectDialog {
            val dialog = SelectDialog().also {
                it.arguments = Bundle().apply {
                    putString("title", title)
                    putInt("position", position)
                    putInt("requestCode", requestCode)
                    putStringArrayList("contents", contents)
                }
            }
            dialog.setFrom(true)
            dialog.show(activity.supportFragmentManager)
            return dialog
        }

        fun showParcelables(activity: FragmentActivity, title: String, position: Int, contents: ArrayList<out Parcelable>, requestCode: Int = -1): SelectDialog {
            val dialog = SelectDialog().also {
                it.arguments = Bundle().apply {
                    putBoolean("isParcelable", true)
                    putString("title", title)
                    putInt("position", position)
                    putInt("requestCode", requestCode)
                    putParcelableArrayList("contents", contents)
                }
            }
            dialog.setFrom(true)
            dialog.show(activity.supportFragmentManager)
            return dialog
        }

        fun show(fragment: Fragment, title: String, position: Int, contents: ArrayList<String>, requestCode: Int = -1): SelectDialog {
            val dialog = SelectDialog().also {
                it.arguments = Bundle().apply {
                    putString("title", title)
                    putInt("position", position)
                    putInt("requestCode", requestCode)
                    putStringArrayList("contents", contents)
                }
            }
            dialog.show(fragment.childFragmentManager)
            return dialog
        }

        fun showParcelables(fragment: Fragment, title: String, position: Int, contents: ArrayList<out Parcelable>, requestCode: Int = -1): SelectDialog {
            val dialog = SelectDialog().also {
                it.arguments = Bundle().apply {
                    putBoolean("isParcelable", true)
                    putString("title", title)
                    putInt("position", position)
                    putInt("requestCode", requestCode)
                    putParcelableArrayList("contents", contents)
                }
            }
            dialog.show(fragment.childFragmentManager)
            return dialog
        }
    }

    override fun getLayoutRes(): Int = R.layout.dialog_select

    override fun initData(v: View, savedState: Bundle?) {
        binding.tvTitle.text = arguments!!.getString("title", "")
        lifecycleScope.launch {
            binding.tvCancel.click().collect {
                dismiss()
            }
        }
        binding.rvList.addItemDecoration(com.lz.comm.widget.RecyclerViewDivider.publicDivider())

        val adapter = if (arguments!!.getBoolean("isParcelable", false)) {
            val contents = arguments!!.getParcelableArrayList<Parcelable>("contents")!!.toList()
            ParcelableSelectAdapter(contents, arguments!!.getInt("position", 0))
        } else {
            val contents = arguments!!.getStringArrayList("contents")!!.toList()
            SelectAdapter(contents, arguments!!.getInt("position", 0))
        }

        adapter.setOnItemClickListener(object : com.lz.library.adapter.BaseAdapter.OnItemClickListener {
            override fun onItemClick(view: View, position: Int) {
                val requestCode = arguments!!.getInt("requestCode")
                if (isFromActivity()) {
                    if (context is OnSelectedListener) {
                        (context as OnSelectedListener).onSelected(this@SelectDialog, position, requestCode)
                    }
                } else {
                    if (parentFragment is OnSelectedListener) {
                        (parentFragment as OnSelectedListener).onSelected(this@SelectDialog, position, requestCode)
                    }
                }
            }
        })
        binding.rvList.adapter = adapter
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog = BottomDialog(requireContext())

    class SelectAdapter(initData: List<String>, private val selectPosition: Int) : SimpleAdapter<String>(initData) {

        override fun getLayoutResId(viewType: Int): Int = R.layout.item_select_public

        override fun onBindViewHolder(vh: SimpleViewHolder, position: Int) {
            val tv = vh.getView<TextView>(R.id.tv_content)
            tv.text = getItem(position)
            tv.isSelected = position == selectPosition
        }
    }

    class ParcelableSelectAdapter(initData: List<Parcelable>, private val selectPosition: Int) : SimpleAdapter<Parcelable>(initData) {

        override fun getLayoutResId(viewType: Int): Int = R.layout.item_select_public

        override fun onBindViewHolder(vh: SimpleViewHolder, position: Int) {
            val tv = vh.getView<TextView>(R.id.tv_content)
            tv.text = getItem(position).toString()
            tv.isSelected = position == selectPosition
        }
    }

    interface OnSelectedListener {

        fun onSelected(dialog: SelectDialog, position: Int, requestCode: Int)
    }
}